/*
 * DispatchMessage.c
 *
 *  Created on: 2025-4-30
 *      Author: Andy.zhai
 */
#include "Commu.h"
#include "CommuRoute.h"
#include "CommuApi.h"

static commu_func_handle_t commu_func_handle[ServiceID_MAX] = {0};

uint8_t CommuRegisterService(ServiceID_t svr_id, HandleMessage_t handleMessage)
{
    if (svr_id >= ServiceID_MAX) {
        rt_kprintf("[Commu]:Service ID is unvaild\n");
        return 1;
    }
    commu_func_handle[svr_id].id = svr_id;
    commu_func_handle[svr_id].handleMessage = handleMessage;
    return 0;
}

/* 0:Success
 * 1:No handle
 * 2:Handle fail
 */
uint8_t SendToMCU(uint8_t *data, uint32_t len)
{
    rt_kprintf("[Commu]:SendtoMcu\n");
    for (uint8_t i=0;i<ServiceID_MAX;i++) {
        if (commu_func_handle[i].id == data[2]) {
            if (commu_func_handle[i].handleMessage(data, len) != 0) {
                rt_kprintf("[Commu]:message process fail\n");
                return 2;
            }
            return 0;
        }
    }
    return 1;
}

extern void SendDataToUsart2(uint8_t *data, uint32_t len);
uint8_t SendToUART(uint8_t *data, uint32_t len)
{
    rt_kprintf("[Commu]:SendtoUsart\n");
    SendDataToUsart2(data, len);
    return 0;
}

uint8_t SendToSPI(uint8_t *data, uint32_t len)
{
    return 0;
}

uint8_t SendToSDIO(uint8_t *data, uint32_t len)
{
    return 0;
}
